﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.Creation;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace YArchitech.HVAC.AuxiliaryCurve
{
	public class ModelLineDraw : AuxiliaryDraw
	{
		public Plane CurPlane { get; set; }

		public ModelLineDraw(ExternalCommandData amdArgu, Dictionary<XYZ, XYZ> dicXArgu, Dictionary<XYZ, XYZ> dicYArgu, ref ICollection<ElementId> idsArgu, Plane planeArgu = null) : base(amdArgu, dicXArgu, dicYArgu, ref idsArgu)
		{
			this.CurPlane = planeArgu;
			this.createDoc = this.m_CmdData.Application.ActiveUIDocument.Document.Create;
		}

		public override bool DrawCurve()
		{
			if (!base.IsInTolerance())
			{
				return false;
			}
			if (this.dicX.Count < 1 && this.dicY.Count < 1)
			{
				return false;
			}
			try
			{
				foreach (KeyValuePair<XYZ, XYZ> keyValuePair in this.dicX)
				{
					ModelCurve modelCurve = this.DrawModelLine(keyValuePair.Key, keyValuePair.Value);
					this.ids.Add(modelCurve.Id);
				}
				foreach (KeyValuePair<XYZ, XYZ> keyValuePair2 in this.dicY)
				{
					ModelCurve modelCurve2 = this.DrawModelLine(keyValuePair2.Key, keyValuePair2.Value);
					this.ids.Add(modelCurve2.Id);
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw new Exception("辅助线绘制范围太小,绘制失败;请调整绘图区域与边距,重新绘制。");
			}
			return true;
		}

		public ModelCurve DrawModelLine(XYZ start, XYZ end)
		{
			Line line = Line.CreateBound(start, end);
			SketchPlane sketchPlane = this.m_CmdData.Application.ActiveUIDocument.Document.CreatYJKSketchPlane(this.CurPlane);
			return this.createDoc.NewModelCurve(line, sketchPlane);
		}

        private Autodesk.Revit.Creation.Document createDoc;
	}
}
